<?php
namespace Lib\Db;

class DbMysql extends Db
{
    public function __construct($config)
    {
        $this->initConfig();
        $this->initInstanceId($config);
        $this->connectDb();
    }

    public function initConfig()
    {
        $this->getConfig();
    }

    /**
     * 连接db
     */
    public function connectDb()
    {
        $dbConfig = $this->config[$this->groupId][$this->instanceId];
        $this->dbLink = new \mysqli($dbConfig['instanceName'], $dbConfig['userName'], $dbConfig['password'], $dbConfig['dbName']);
        $this->dbLink->query("SET NAMES utf8");
        if ($this->dbLink->connect_errno) {
            $this->writeLog($this->dbLink->connect_errno, $this->dbLink->connect_error, $dbConfig);
        }
    }

    /**
     *  执行select
     */
    public function execQuery($sql)
    {
        $this->writeLog('0', 'success', $sql);

        $result = $this->dbLink->query($sql);
        if ($result === false) {
            $retData = false;
            $this->writeLog($this->dbLink->errno, $this->dbLink->error, $sql);
        } else {
            if ($result->num_rows == 0) {
                $retData = ['rows' => 0, 'data' => []];
            } else {
                $retData = ['rows' => $result->num_rows, 'data' => []];
                while ($row = $result->fetch_assoc()) {
                    $retData['data'][] = $row;
                }
            }
            $result->free();
        }
        recordLog("result:" . json_encode($retData));
        return $retData;
    }

    /**
     * 执行 insert update delete replace
     */
    public function execUpdate($sql, $getId = 0)
    {
        $this->writeLog('0', 'success', $sql);

        $result = $this->dbLink->query($sql);
        if ($result === false) {
            $retData = false;
            $this->writeLog($this->dbLink->errno, $this->dbLink->error, $sql);
        } else {
            if ($this->dbLink->affected_rows == 0) {
                $retData = false;
            } else {
                if ($this->dbLink->insert_id != 0 || $getId == 1) {
                    $retData = $this->dbLink->insert_id;
                } else {
                    $retData = $this->dbLink->affected_rows;
                }
            }
        }
        recordLog("result:" . json_encode($retData));

        return $retData;
    }

    /**
     * 执行事务
     */
    public function execTrans($sqlArr)
    {
        $this->writeLog('0', 'success', $sqlArr);

        $this->dbLink->autocommit(false);
        foreach ($sqlArr as $sql) {
            $result = $this->dbLink->query($sql);
            if ($result === false || $this->dbLink->affected_rows == 0) {
                if ($result === false) {
                    $this->writeLog($this->dbLink->errno, $this->dbLink->error, $sql);
                } else {
                    $this->writeLog('-910', 'affected_rows 0!', $sql);
                }
                $this->dbLink->rollback();
                return false;
            }
        }
        $rtn = $this->dbLink->commit();
        if (!$rtn) {
            $this->writeLog($this->dbLink->errno, $this->dbLink->error, $sql);
            $this->dbLink->rollback();
            return false;
        }
        return true;
    }

    public function __destruct()
    {
        $this->dbLink->close();
    }
}
